import * as Cesium from 'cesium';

export class GeometryDrawing {
  constructor(cesiumManager) {
    this.cesiumManager = cesiumManager;
    this.drawHandler = null;
    this.activeEntity = null;
    this.tempEntities = [];
  }

  // 添加材质线
  addMaterialLine() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    const positions = [];
    this.activeEntity = viewer.entities.add({
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          if (positions.length >= 2) {
            return positions;
          }
          return null;
        }, false),
        width: 5,
        material: new Cesium.PolylineGlowMaterialProperty({
          color: Cesium.Color.CYAN,
          glowPower: 0.2,
          taperPower: 1.0
        })
      }
    });
    
    this.tempEntities.push(this.activeEntity);
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        positions.push(cartesian);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    
    this.drawHandler.setInputAction((click) => {
      if (positions.length < 2) {
        viewer.entities.remove(this.activeEntity);
        this.tempEntities.pop();
      } else {
        this.activeEntity.polyline.positions = positions.slice();
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
      this.activeEntity = null;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  // 添加空洞面
  addPolygonWithHole() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    // 外环顶点
    const outerRing = [];
    // 内环顶点
    const innerRing = [];
    let isDrawingOuter = true;
    
    this.activeEntity = viewer.entities.add({
      polygon: {
        hierarchy: new Cesium.CallbackProperty(() => {
          if (outerRing.length >= 3) {
            const holes = innerRing.length >= 3 ? [new Cesium.PolygonHierarchy(innerRing)] : [];
            return new Cesium.PolygonHierarchy(outerRing, holes);
          }
          return null;
        }, false),
        material: Cesium.Color.BLUE.withAlpha(0.5),
        outline: true,
        outlineColor: Cesium.Color.BLACK
      }
    });
    
    this.tempEntities.push(this.activeEntity);
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        if (isDrawingOuter) {
          outerRing.push(cartesian);
        } else {
          innerRing.push(cartesian);
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    
    this.drawHandler.setInputAction((click) => {
      // 切换内外环绘制
      if (isDrawingOuter && outerRing.length >= 3) {
        isDrawingOuter = false;
      }
    }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
    
    this.drawHandler.setInputAction((click) => {
      if (outerRing.length < 3) {
        viewer.entities.remove(this.activeEntity);
        this.tempEntities.pop();
      } else {
        const holes = innerRing.length >= 3 ? [new Cesium.PolygonHierarchy(innerRing)] : [];
        this.activeEntity.polygon.hierarchy = new Cesium.PolygonHierarchy(outerRing, holes);
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
      this.activeEntity = null;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  // 添加水面动画
  addWaterSurface() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        // 创建一个水面效果的面
        const entity = viewer.entities.add({
          position: cartesian,
          ellipse: {
            semiMajorAxis: 50000.0,
            semiMinorAxis: 50000.0,
            material: new Cesium.ImageMaterialProperty({
              image: '/images/water.png', // 需要提供水面纹理图片
              transparent: true
            }),
            height: 0.0
          }
        });
        this.tempEntities.push(entity);
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  // 添加覆盖面
  addOverlayPolygon() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    const positions = [];
    this.activeEntity = viewer.entities.add({
      polygon: {
        hierarchy: new Cesium.CallbackProperty(() => {
          if (positions.length >= 3) {
            return new Cesium.PolygonHierarchy(positions);
          }
          return null;
        }, false),
        material: new Cesium.ColorMaterialProperty(
          Cesium.Color.fromBytes(0, 255, 0, 100)
        ),
        classificationType: Cesium.ClassificationType.TERRAIN
      }
    });
    
    this.tempEntities.push(this.activeEntity);
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        positions.push(cartesian);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    
    this.drawHandler.setInputAction((click) => {
      if (positions.length < 3) {
        viewer.entities.remove(this.activeEntity);
        this.tempEntities.pop();
      } else {
        this.activeEntity.polygon.hierarchy = new Cesium.PolygonHierarchy(positions.slice());
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
      this.activeEntity = null;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  // 添加圆
  addCircle() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    let center = null;
    let radius = 0;
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        if (!center) {
          center = cartesian;
        } else {
          radius = Cesium.Cartesian3.distance(center, cartesian);
          const entity = viewer.entities.add({
            position: center,
            ellipse: {
              semiMajorAxis: radius,
              semiMinorAxis: radius,
              material: Cesium.Color.BLUE.withAlpha(0.5),
              outline: true,
              outlineColor: Cesium.Color.BLACK
            }
          });
          this.tempEntities.push(entity);
          this.drawHandler.destroy();
          this.drawHandler = null;
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  // 添加矩形
  addRectangle() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    let startCorner = null;
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        if (!startCorner) {
          startCorner = cartesian;
        } else {
          const entity = viewer.entities.add({
            rectangle: {
              coordinates: new Cesium.CallbackProperty(() => {
                return Cesium.Rectangle.fromCartesianArray([startCorner, cartesian]);
              }, false),
              material: Cesium.Color.RED.withAlpha(0.5),
              outline: true,
              outlineColor: Cesium.Color.BLACK
            }
          });
          this.tempEntities.push(entity);
          this.drawHandler.destroy();
          this.drawHandler = null;
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  // 添加进攻箭头
  addAttackArrow() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    const positions = [];
    this.activeEntity = viewer.entities.add({
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          if (positions.length >= 2) {
            // 简化实现，实际应该计算箭头形状
            return positions;
          }
          return null;
        }, false),
        width: 3,
        material: Cesium.Color.RED
      }
    });
    
    this.tempEntities.push(this.activeEntity);
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        positions.push(cartesian);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    
    this.drawHandler.setInputAction((click) => {
      if (positions.length < 2) {
        viewer.entities.remove(this.activeEntity);
        this.tempEntities.pop();
      } else {
        this.activeEntity.polyline.positions = positions.slice();
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
      this.activeEntity = null;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  // 添加双箭头
  addDoubleArrow() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    const positions = [];
    this.activeEntity = viewer.entities.add({
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          if (positions.length >= 2) {
            // 简化实现，实际应该计算双箭头形状
            return positions;
          }
          return null;
        }, false),
        width: 3,
        material: Cesium.Color.YELLOW
      }
    });
    
    this.tempEntities.push(this.activeEntity);
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        positions.push(cartesian);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    
    this.drawHandler.setInputAction((click) => {
      if (positions.length < 2) {
        viewer.entities.remove(this.activeEntity);
        this.tempEntities.pop();
      } else {
        this.activeEntity.polyline.positions = positions.slice();
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
      this.activeEntity = null;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  // 添加直箭头
  addStraightArrow() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    let startPoint = null;
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        if (!startPoint) {
          startPoint = cartesian;
        } else {
          // 创建箭头
          const entity = viewer.entities.add({
            polyline: {
              positions: [startPoint, cartesian],
              width: 5,
              material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.BLUE)
            }
          });
          this.tempEntities.push(entity);
          this.drawHandler.destroy();
          this.drawHandler = null;
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  // 添加燕尾箭头
  addSwallowtailArrow() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    const positions = [];
    this.activeEntity = viewer.entities.add({
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          if (positions.length >= 2) {
            // 简化实现，实际应该计算燕尾箭头形状
            return positions;
          }
          return null;
        }, false),
        width: 3,
        material: Cesium.Color.GREEN
      }
    });
    
    this.tempEntities.push(this.activeEntity);
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        positions.push(cartesian);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    
    this.drawHandler.setInputAction((click) => {
      if (positions.length < 2) {
        viewer.entities.remove(this.activeEntity);
        this.tempEntities.pop();
      } else {
        this.activeEntity.polyline.positions = positions.slice();
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
      this.activeEntity = null;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  // 添加聚集地
  addAssemblyArea() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    const positions = [];
    this.activeEntity = viewer.entities.add({
      polygon: {
        hierarchy: new Cesium.CallbackProperty(() => {
          if (positions.length >= 3) {
            return new Cesium.PolygonHierarchy(positions);
          }
          return null;
        }, false),
        material: new Cesium.StripeMaterialProperty({
          evenColor: Cesium.Color.RED.withAlpha(0.5),
          oddColor: Cesium.Color.YELLOW.withAlpha(0.5),
          repeat: 5.0
        }),
        outline: true,
        outlineColor: Cesium.Color.BLACK
      }
    });
    
    this.tempEntities.push(this.activeEntity);
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        positions.push(cartesian);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    
    this.drawHandler.setInputAction((click) => {
      if (positions.length < 3) {
        viewer.entities.remove(this.activeEntity);
        this.tempEntities.pop();
      } else {
        this.activeEntity.polygon.hierarchy = new Cesium.PolygonHierarchy(positions.slice());
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
      this.activeEntity = null;
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  // 添加文本标绘
  addTextPlot() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
    }
    
    this.drawHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    
    this.drawHandler.setInputAction((click) => {
      const cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
      if (cartesian) {
        // 这里应该弹出输入框让用户输入文本
        const text = prompt("请输入文本:", "标绘文本");
        if (text) {
          const entity = viewer.entities.add({
            position: cartesian,
            label: {
              text: text,
              font: '18px sans-serif',
              fillColor: Cesium.Color.WHITE,
              outlineColor: Cesium.Color.BLACK,
              outlineWidth: 2,
              style: Cesium.LabelStyle.FILL_AND_OUTLINE,
              verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
              pixelOffset: new Cesium.Cartesian2(0, -10)
            }
          });
          this.tempEntities.push(entity);
        }
      }
      this.drawHandler.destroy();
      this.drawHandler = null;
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }

  getTempEntities() {
    return this.tempEntities;
  }

  setTempEntities(entities) {
    this.tempEntities = entities;
  }

  clear() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    this.tempEntities.forEach(entity => {
      viewer.entities.remove(entity);
    });
    
    this.tempEntities = [];
    
    if (this.drawHandler) {
      this.drawHandler.destroy();
      this.drawHandler = null;
    }
    
    this.activeEntity = null;
  }
}